package com.abc.recycling.mapper;

import com.abc.recycling.domain.Evaluation;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EvaluationMapper {
    
    // 插入评价记录
    @Insert("INSERT INTO evaluation(order_id, user_id, target_id, rating, content, create_time, status, update_time) " +
            "VALUES(#{orderId}, #{userId}, #{collectorId}, #{rating}, #{comment}, #{createTime}, #{status}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Evaluation evaluation);
    
    // 更新评价记录(主要用于回复评价)
    @Update("UPDATE evaluation SET reply = #{reply}, reply_time = #{replyTime}, status = #{status}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int update(Evaluation evaluation);
    
    // 根据ID查询评价记录
    @Select("SELECT * FROM evaluation WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "orderId", column = "order_id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "collectorId", column = "target_id"),
        @Result(property = "comment", column = "content"),
        @Result(property = "user", column = "user_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById")),
        @Result(property = "collector", column = "target_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById")),
        @Result(property = "order", column = "order_id", 
                one = @One(select = "com.abc.recycling.mapper.OrderMapper.selectById"))
    })
    Evaluation findById(Long id);
    
    // 根据订单ID查询评价记录
    @Select("SELECT * FROM evaluation WHERE order_id = #{orderId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "orderId", column = "order_id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "collectorId", column = "target_id"),
        @Result(property = "comment", column = "content"),
        @Result(property = "user", column = "user_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById")),
        @Result(property = "collector", column = "target_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById"))
    })
    Evaluation findByOrderId(Long orderId);
    
    // 根据回收员ID查询评价记录
    @Select("SELECT * FROM evaluation WHERE target_id = #{targetId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "orderId", column = "order_id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "collectorId", column = "target_id"),
        @Result(property = "comment", column = "content"),
        @Result(property = "user", column = "user_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById")),
        @Result(property = "collector", column = "target_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById")),
        @Result(property = "order", column = "order_id", 
                one = @One(select = "com.abc.recycling.mapper.OrderMapper.selectById"))
    })
    List<Evaluation> findByTargetId(Long targetId);
    
    // 统计回收员的平均评分
    @Select("SELECT AVG(rating) FROM evaluation WHERE target_id = #{targetId}")
    Double calculateAverageRating(Long targetId);
    
    // 多条件搜索评价记录
    @Select({
        "SELECT * FROM evaluation WHERE 1=1",
        "<if test='userId != null'>",
        "  AND user_id = #{userId}",
        "</if>",
        "<if test='targetId != null'>",
        "  AND target_id = #{targetId}",
        "</if>",
        "<if test='status != null'>",
        "  AND status = #{status}",
        "</if>",
        "<if test='minRating != null'>",
        "  AND rating >= #{minRating}",
        "</if>",
        "<if test='maxRating != null'>",
        "  AND rating <= #{maxRating}",
        "</if>",
        "ORDER BY create_time DESC"
    })
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "orderId", column = "order_id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "collectorId", column = "target_id"),
        @Result(property = "comment", column = "content"),
        @Result(property = "user", column = "user_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById")),
        @Result(property = "collector", column = "target_id", 
                one = @One(select = "com.abc.recycling.mapper.UserMapper.findById")),
        @Result(property = "order", column = "order_id", 
                one = @One(select = "com.abc.recycling.mapper.OrderMapper.selectById"))
    })
    List<Evaluation> search(Long userId, Long targetId, Integer status, Double minRating, Double maxRating);
} 